package com.island.practice.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.island.practice.entity.Role;

/**
 * The sevelet class to insert Role into database
 */

public class CreateRoleServlet extends AbstractServlet{

	private static final String SQL_INSERT_ROLE = 
			"INSERT INTO ROLE (ROLE_ID, ROLE_NAME) VALUES(?, ?)";
	private static final String SQL_QUERY_ROLE = "SELECT ROLE_ID FROM ROLE WHERE ROLE_ID = ?";
	
	/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws IOException 
     */
	
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
		    throws ServletException, IOException {
		
		String roleId		= request.getParameter("roleId");
        String roleName 	= request.getParameter("roleName");
		        
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet roleResults = null;
        Role role = new Role();
        
        try {
			connection = this.getConnection();
			//HEAD: transaction boundary
	        connection.setAutoCommit(false);
	        statement = connection.prepareStatement(SQL_QUERY_ROLE);
    		statement.setString(1, roleId);
    		roleResults = statement.executeQuery();
	         		
    		if (roleResults.next())
    		{
    			role.setRoleId(roleId);
				role.setRoleName(roleName);
    			request.setAttribute("warning", 0);
    			request.setAttribute("role", role);
    			request.getRequestDispatcher("/Role/CreateRole.jsp").forward(request, response);
    		}
    		else{
    			
    			//insert role
    	    	insertRole(roleId, roleName, connection);
    	    	request.getRequestDispatcher("/Role/ListRole").forward(request, response);
    		}
    		
    		
        } catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("insert role failed", e);
		} finally {
			closeStatement(statement);
			//TAIL: transaction boundary
			closeConnection(connection);
		}
        
        
	}
	
	private void insertRole(String roleId, String roleName, Connection connection)
			throws SQLException {
		PreparedStatement statement = null;
		
		try {
			connection.setAutoCommit(false);
			statement = connection.prepareStatement(SQL_INSERT_ROLE);
			statement.setString(1, roleId);
			statement.setString(2, roleName);
			statement.execute();
			connection.commit();
		} catch (SQLException e)
		{	
			try {
            connection.rollback();
			} catch (SQLException e1) {
            e1.printStackTrace();
       }			
			e.printStackTrace();
		} finally {
			closeStatement(statement);
		}
	}
	
	/** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
}
